package 银河英雄传说;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class Main {
    static int N = 30001;
    static int[] p = new int[N];
    static int[] size = new int[N];
    static int[] cnt = new int[N];

    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        int T = Integer.parseInt(in.readLine());
        for (int i = 1; i <= N - 1; i++) {
            p[i] = i;
            size[i] = 1;
        }
        while (T-- > 0) {
            String[] s = in.readLine().split(" ");
            int a = Integer.parseInt(s[1]);
            int b = Integer.parseInt(s[2]);
            int pa = find(a), pb = find(b);
            if (s[0].equals("M")) {
                if (pa != pb) {
                    cnt[pa] = size[pb];
                    size[pb] += size[pa];
                    p[pa] = pb;
                }
            } else {
                if (pa == pb) {
                    System.out.println(Math.max(0, Math.abs(cnt[a] - cnt[b]) - 1));
                } else {
                    System.out.println(-1);
                }
            }
        }
    }

    private static int find(int x) {
        if (x != p[x]) {
            int root = find(p[x]);
            cnt[x] += cnt[p[x]];
            p[x] = root;
        }
        return p[x];
    }
}
